package com.vipboot.platform.system.controller.rbac;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.vipboot.framework.core.constant.TenantConst;
import com.vipboot.framework.core.domain.R;
import com.vipboot.framework.core.validation.AddGroup;
import com.vipboot.framework.core.validation.EditGroup;
import com.vipboot.framework.log.annotation.Log;
import com.vipboot.framework.log.enums.BusinessType;
import com.vipboot.framework.satoken.helper.LoginHelper;
import com.vipboot.platform.system.crud.service.rbac.SysMenuService;
import com.vipboot.platform.system.domain.bo.SysMenuBo;
import com.vipboot.platform.system.domain.vo.SysMenuVo;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表  前端控制器
 * </p>
 *
 * @author daze
 * @since 2024-12-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    /**
     * 获取菜单列表
     */
    @SaCheckRole(value = {
            TenantConst.SuperAdminRoleKey,
            TenantConst.TenantAdminRoleKey
    }, mode = SaMode.OR)
    @SaCheckPermission("sysMenu:list")
    @GetMapping("/list")
    public R<List<SysMenuVo>> list(@ModelAttribute SysMenuBo bo) {
        return R.ok(sysMenuService.list(bo, LoginHelper.getUserId()));
    }


    @SaCheckRole(TenantConst.SuperAdminRoleKey)
    @SaCheckPermission("sysMenu:save")
    @Log(module = "菜单管理", title = "添加菜单", businessType = BusinessType.Insert)
    @PostMapping("/save")
    public R<Void> save(@Validated(AddGroup.class)
                        @RequestBody SysMenuBo bo) {
        return sysMenuService.save(bo) ? R.ok() : R.fail();
    }

    @SaCheckRole(TenantConst.SuperAdminRoleKey)
    @SaCheckPermission("sysMenu:update")
    @Log(module = "菜单管理", title = "更新菜单", businessType = BusinessType.Update)
    @PostMapping("/update")
    public R<Void> update(@Validated(EditGroup.class)
                          @RequestBody SysMenuBo bo) {
        return sysMenuService.update(bo) ? R.ok() : R.fail();
    }

    @SaCheckRole(TenantConst.SuperAdminRoleKey)
    @SaCheckPermission("sysMenu:remove")
    @Log(module = "菜单管理", title = "删除菜单", businessType = BusinessType.Delete)
    @PostMapping("/remove/{menuId}")
    public R<Void> remove(@NotNull(message = "主键不能为空")
                          @PathVariable Long menuId) {
        return sysMenuService.removeById(menuId) ? R.ok() : R.fail();
    }

}
